package cn.tedu.carrent.user.service.impl;

import cn.tedu.carrent.base.exception.ServiceException;
import cn.tedu.carrent.base.response.StatusCode;
import cn.tedu.carrent.user.mapper.UserMapper;
import cn.tedu.carrent.user.pojo.dto.UserLoginParam;
import cn.tedu.carrent.user.pojo.dto.UserQuary;
import cn.tedu.carrent.user.pojo.dto.UserSaveParam;
import cn.tedu.carrent.user.pojo.entity.User;
import cn.tedu.carrent.user.pojo.vo.UserVO;
import cn.tedu.carrent.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
//@Service注解表示当前类为业务层实现类
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("登录业务，userLoginParam={}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException( StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }
    public void saveUser(UserSaveParam userSaveParam){
        log.debug("用户 userSaveParam:"+userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
//        新增用户
        if(user.getId()==null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{
//            user.setUpdateTime(new Date());
//            userMapper.update(user);
        }
    }
    public List<UserVO> selectUser(UserQuary userQuary){
        log.debug("查询用户业务，userQuary={}", userQuary);
        List<UserVO> list = userMapper.selectUser(userQuary);
        return list;
    }
//    public void resetPassword(Long id){
//        User user = new User();
//        user.setId(id);
//        user.setPassword("root");
//        user.setUpdateTime(new Date());
//        userMapper.update(user);
//    }
//    public void updateStatus(Long userId,String status){
//        User user = new User();
//        user.setId(userId);
//        user.setStatus(status);
//        user.setUpdateTime(new Date());
//        userMapper.update(user);
//    }
//    public void deleteUser(Long userId){
//        userMapper.deleteById(userId);
//    }
//
//    @Override
//    public List<UserVO> selectAudit(Long parentId) {
//        log.debug("查询审批人业务，parentId="+parentId);
////        准备一个空集合，用来保存查出的多个审核人
//        ArrayList<UserVO> list = new ArrayList<>();
////        现根据传过来的上级id将直属领导查出来，并装到上方的空集合里
//        UserVO auditUser1 = userMapper.selectAudit(parentId);
//        list.add(auditUser1);
////        如果有直属领导，且直属领导还有上级
//        if(auditUser1!=null && auditUser1.getParentId()!=null){
////            根据直属领导的上级id将上上级领导查出来，并装到上方空集合里
//            UserVO auditUser2 = userMapper.selectAudit(auditUser1.getParentId());
//            list.add(auditUser2);
//        }
////        将准备好的审批人集合返回给controller
//        return list;
//    }
}
